package com.one.blocks.rbac.controller.convert;

import com.one.blocks.rbac.controller.convert.worker.RelationDomainConvert;
import com.one.blocks.rbac.controller.convert.worker.TimeConvert;
import com.one.blocks.rbac.controller.vo.SysRoleGrantOrgVo;
import com.one.blocks.rbac.controller.vo.SysRoleOrgVo;
import com.one.blocks.rbac.domain.SysRoleOrg;
import com.one.blocks.security.util.SubjectHelper;
import org.apache.commons.collections4.CollectionUtils;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;

/**
 * @author <a href="mailto:idler41@163.con">idler41</a> created on 2022-05-06 10:54:42
 */
@Mapper(uses = {TimeConvert.class}, imports = {SubjectHelper.class})
public interface SysRoleOrgConvert extends RelationDomainConvert<SysRoleOrg, SysRoleOrgVo> {

    SysRoleOrgConvert INSTANCE = Mappers.getMapper(SysRoleOrgConvert.class);

    /**
     * 数据对象转换
     *
     * @param sysRoleGrantVo 授权参数
     * @return 存储关联数据
     */
    default List<SysRoleOrg> grantVoToEntity(SysRoleGrantOrgVo sysRoleGrantVo) {
        if (CollectionUtils.isEmpty(sysRoleGrantVo.getOrgIds())) {
            return null;
        }
        List<SysRoleOrg> dataToInsert = new ArrayList<>(sysRoleGrantVo.getOrgIds().size());
        for (Integer orgId : sysRoleGrantVo.getOrgIds()) {
            SysRoleOrg sysRoleOrg = new SysRoleOrg();

            sysRoleOrg.setRoleId(sysRoleGrantVo.getRoleId());
            sysRoleOrg.setOrgId(orgId);
            sysRoleOrg.setCreateBy(SubjectHelper.getSubjectId());
            sysRoleOrg.setCreateTime(SubjectHelper.getAccessTime());

            dataToInsert.add(sysRoleOrg);
        }
        return dataToInsert;
    }
}